﻿using UFrame;
using UnityEngine;
using UnityEngine.UI;

public class UIPopText : MonoBehaviour
{

    public Text mText;
    public RectTransform mTransform;

    bool mIsWorking;

    FloatTimeCounter mTime;

    Vector2 mStartPos;
    Vector2 mEndPos;
    public static float popTime = 1f;
    public static float bulletTime = 8f;

    private PopText mPop = null;

    public void Awake()
    {
        mTime = new FloatTimeCounter();
        if (mTransform == null)
        {
            mTransform = GetComponent<RectTransform>();
        }

        if (mText == null)
        {
            mText = GetComponent<Text>();
        }
    }


    public void SetInfo(PopText pop)
    {
        mPop = pop;
        mText.text = pop.text;
        mIsWorking = true;
        mTime.ResetCounter();


        RectTransform root = UIMa.UIRoot.GetComponent<RectTransform>();
        //Debug.Log(root.name + " " + root.sizeDelta.x + " " + root.sizeDelta.y);

        float halfWidth = root.sizeDelta.x / 2;
        float halfHeight = root.sizeDelta.y / 2;

        if (pop.IsBullet)
        {
            float y = Random.Range(0, halfHeight - 30);
            mStartPos = mTransform.anchoredPosition = new Vector2(root.sizeDelta.x, y);
            mEndPos = new Vector2(-root.sizeDelta.x, y);

        }
        else
        {
            mEndPos = mStartPos = new Vector2(0, 0);
            mEndPos.y += 100;
            mTransform.anchoredPosition = mStartPos;
        }


    }

    public void Update()
    {
        if (mIsWorking)
        {
            if (mPop.IsBullet)
            {
                mTransform.anchoredPosition = Vector2.Lerp(mStartPos, mEndPos, mTime.GetPassTime() / bulletTime);
                if (mTransform.anchoredPosition.x <= mEndPos.x)
                {
                    mTransform.gameObject.SetActive(false);
                    mIsWorking = false;
                    UIPop.ReuseNode(this);
                    Debug.Log("弹幕回收完毕");
                }
            }
            else
            {
                if (mTime.GetPassTime() > popTime)
                {
                    mTransform.gameObject.SetActive(false);
                    mIsWorking = false;
                    UIPop.ReuseNode(this);
                }
                else
                {
                    mTransform.anchoredPosition = Vector2.Lerp(mStartPos, mEndPos, mTime.GetPassTime() / popTime);
                }
            }
        }

    }
}
